package com.yocard.yxyd.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yocard.yxyd.R;
import com.yocard.yxyd.utils.util.LogUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

public abstract class BaseTabAdapter<T> extends RecyclerView.Adapter<BaseTabAdapter.ViewHolder> {
    private List<T> datas;
    private List<T> selectedList;
    private OnTabSelectListener onTabSelectListener;

    public BaseTabAdapter() {
//        datas = new ArrayList<>();
        this(new ArrayList<>());
    }

    public BaseTabAdapter(List<T> data) {
        this.datas = data;
        selectedList = new ArrayList<>();
    }

    public void replaceDatas(List<T> list) {
        datas.clear();
        datas.addAll(list);
        notifyDataSetChanged();
    }

    public List<T> getSelectedList() {
        return selectedList;
    }

    public void setSelectedList(List<T> selectedList) {
        this.selectedList = selectedList;
    }

    public void setOnTabSelectListener(OnTabSelectListener onTabSelectListener) {
        this.onTabSelectListener = onTabSelectListener;
    }

    @NonNull
    @Override
    public BaseTabAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_tab, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final BaseTabAdapter.ViewHolder viewHolder, int position) {
        T data = datas.get(position);
        viewHolder.tvTab.setText(getTabText(data));
        boolean contain = tabSelected(data);
        if (contain) {
            viewHolder.tvTab.setSelected(true);
            viewHolder.ivSelected.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tvTab.setSelected(false);
            viewHolder.ivSelected.setVisibility(View.GONE);
        }
        viewHolder.tvTab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                view.setSelected(!view.isSelected());
                boolean selected = view.isSelected();
//                viewHolder.tvTab.setSelected(selected);
                if (!selected) {
//                    selectedList.remove(data);
                    viewHolder.ivSelected.setVisibility(View.GONE);
                } else {
//                    selectedList.add(data);
                    viewHolder.ivSelected.setVisibility(View.VISIBLE);
                }
                if (onTabSelectListener != null)
                    onTabSelectListener.onTabSelect(position, selected);
            }
        });
    }

    public abstract String getTabText(T data);
    public abstract boolean tabSelected(T data);

    public interface OnTabSelectListener {
        void onTabSelect(int position, boolean selected);
    }

    public T getItem(int postion) {
        return datas.get(postion);
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_tab)
        TextView tvTab;
        @BindView(R.id.iv_selected)
        ImageView ivSelected;

        public TextView getTvTab() {
            return tvTab;
        }

        public ImageView getIvSelected() {
            return ivSelected;
        }

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
